package cn.kgc.crowdfundingadmin03.service;

import cn.kgc.crowdfundingadmin03.common.CrowdFundingUtils;
import cn.kgc.crowdfundingadmin03.mapper.AdvertisementMapper;

import cn.kgc.crowdfundingadmin03.pojo.AdminExample;
import cn.kgc.crowdfundingadmin03.pojo.Advertisement;
import cn.kgc.crowdfundingadmin03.pojo.AdvertisementExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AdvertisementServiceImpl implements AdvertisementService {

    @Resource
    AdvertisementMapper advertisementMapper;

    @Override
    public PageInfo<Advertisement> queryForKeywordSearch(Integer pageNum, Integer pageSize, String keyword) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy("id desc");
        if(CrowdFundingUtils.stringEffective(keyword)){
            AdvertisementExample example=new AdvertisementExample();
            AdvertisementExample.Criteria criteria1 = example.createCriteria();
            criteria1.andIconpathLike("%"+keyword+"%");
            AdvertisementExample.Criteria criteria2 = example.createCriteria();
            criteria2.andNameLike("%"+keyword+"%");
            AdvertisementExample.Criteria criteria3 = example.createCriteria();
            criteria3.andUrlLike("%"+keyword+"%");
            example.or(criteria2);
            example.or(criteria3);
            List<Advertisement> list=advertisementMapper.selectByExample(example);
            PageInfo<Advertisement> pageInfo=new PageInfo<>(list);
            return pageInfo;
        }else{
            /*查询全部   如果值为空的 则查询全部 判断*/
            List<Advertisement> list = advertisementMapper.selectByExample(null);
            PageInfo<Advertisement> pageInfo=new PageInfo<>(list);
            return  pageInfo;
        }
    }

    @Override
    public void batchRemove(List<Integer> adminIdList) {
        AdvertisementExample example=new AdvertisementExample();
        AdvertisementExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(adminIdList);
        advertisementMapper.deleteByExample(example);
    }

    /*添加广告信息*/
    @Override
    public void saveAdmin(Advertisement advertisement) {

        advertisementMapper.insert(advertisement);
    }

    @Override
    public Advertisement getAdminById(Integer adminId) {
        return advertisementMapper.selectByPrimaryKey(adminId);
    }

    @Override
    public void updateAdmin(Advertisement admin) {
        advertisementMapper.updateByPrimaryKeySelective(admin);
    }
}
